﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FirmaTransportowa
{
    public class DriverPanelPresenter
    {
        private User user;
        private DriverPanelForm driverPanelView;
        private DriverTransportsListForm driverTransportsListView;
        private DriverBeginTransportForm driverBeginTransportView;
        private DriverActualTransportForm driverActualTransportView;

        public DriverPanelPresenter(User user)
        {
            this.user = user;
            this.driverPanelView = new DriverPanelForm(this);
            this.showLoggedUserName();
            this.driverPanelView.Show();
        }

        public void clean() 
        {
            if (driverTransportsListView != null) driverTransportsListView.Dispose();
            if (driverBeginTransportView != null) driverBeginTransportView.Dispose();
            if (driverActualTransportView != null) driverActualTransportView.Dispose();
            if (driverPanelView != null) driverPanelView.Dispose();
        }

        public void startTransport() 
        {
            DataGridViewRow row = driverBeginTransportView.transportsGridView.SelectedRows[0];

            Transport transport = Transport.getById(int.Parse(row.Cells[0].Value.ToString()));
            transport.startTransport();

            MessageBox.Show("Rozpoczęto transport!");
            driverBeginTransportView.Close();
        }

        public void finishTransport() 
        {
            Transport transport = getTransportInProgress();

            List<Package> packages = Package.getPackagesByTransport(transport);

            bool finish = true;

            foreach (Package package in packages)
            {
                if (package.status == PackageStatus.NEW || package.status == PackageStatus.DELIVERED)
                {
                    finish = false;
                }
            }

            if (finish)
            {
                transport.finishTransport();

                MessageBox.Show("Zakończono transport!");
                driverActualTransportView.Close();
            }
            else if (transport.destination_city == "sortownia")
            {
                MessageBox.Show("Nie można zakończyć transportu, posiadasz paczki do odebrania!");
            }
            else
            {
                MessageBox.Show("Nie można zakończyć transportu, posiadasz paczki do dostarczenia!");
            }
        }

        public void showTransportsListView()
        {
            if (driverTransportsListView == null || driverTransportsListView.IsDisposed)
            {
                driverTransportsListView = new DriverTransportsListForm(this);
                displayTransportsList();
                driverTransportsListView.Show();
            }
        }

        public void showBeginTransportView()
        {
            // jeżeli istnieje rozpoczęty transport dla danego użytkownika okno nie wyświetli się
            if (hasTransportInProgress(user))
            {
                MessageBox.Show("Posiadasz rozpoczęty transport!");
            }
            else if (driverBeginTransportView == null || driverBeginTransportView.IsDisposed)
            {
                driverBeginTransportView = new DriverBeginTransportForm(this);
                displayTransportsList(TransportStatus.PLANNED);
                driverBeginTransportView.Show();
            }
        }

        public void showActualTransportView()
        {
            // jeżeli nie istnieje rozpoczęty transport dla danego użytkownika okno nie wyświetli się
            if (!hasTransportInProgress(user))
            {
                MessageBox.Show("Aktualnie nie posiadasz rozpoczętego transportu!");
            }
            else if (driverActualTransportView == null || driverActualTransportView.IsDisposed)
            {
                driverActualTransportView = new DriverActualTransportForm(this);
                showActualTransportPackages();
                showActualTransportData();
                driverActualTransportView.Show();
            }
        }

        private bool hasTransportInProgress(User u) {
            List<Transport> transports = Transport.list();
            foreach (Transport transport in transports)
            {
                if (transport.driver.id == u.worker.id && transport.status == TransportStatus.IN_PROGRESS)
                {
                    return true;
                }
            }
            return false;
        }

        private Transport getTransportInProgress() {
            List<Transport> transports = Transport.list();
            foreach (Transport transport in transports)
            {
                if (transport.driver.id == user.worker.id && transport.status == TransportStatus.IN_PROGRESS)
                {
                    return transport;
                }
            }
            return new Transport();
        }

        private void displayTransportsList() {
            driverTransportsListView.transportsGridView.DataSource = Transport.getTransportsListForDriver(user.id);
            driverTransportsListView.showDataTable();
        }

        private void displayTransportsList(TransportStatus status)
        {
            driverBeginTransportView.transportsGridView.DataSource = Transport.getTransportsListForDriver(user.id, status);
            driverBeginTransportView.showDataTable();
        }

        private void showLoggedUserName() 
        {
            driverPanelView.driverName.Text = user.worker.name + " " + user.worker.surname;
        }

        private void showActualTransportData() 
        {
            Transport transport = getTransportInProgress();

            driverActualTransportView.startPlace.Text = transport.start_city;
            driverActualTransportView.endPlace.Text = transport.destination_city;
            driverActualTransportView.startTime.Text = transport.transport_start_time.ToString();
            driverActualTransportView.dispatcher.Text = transport.dispatcher.name + " " + transport.dispatcher.surname;
            driverActualTransportView.distance.Text = transport.distance.ToString();

            driverActualTransportView.showDataTable(transport);

            if (transport.destination_city == "sortownia")
            {
                driverActualTransportView.processPackages.Text = "Odbierz paczki";
            }
            else 
            {
                driverActualTransportView.processPackages.Text = "Dostarcz paczki";
            }
        }

        private void showActualTransportPackages() {
            Transport transport = getTransportInProgress();

            driverActualTransportView.actualTransportPackages.DataSource = Package.getPackagesForActualTransport(transport);
        }

        // funkcja zmienia statusy paczek i odświeża listę paczek do odebrania
        public void processPackages() {
            if (driverActualTransportView.actualTransportPackages.SelectedRows.Count == 0)
            {
                MessageBox.Show("Nie wybrano żadnych paczek");
            }
            else
            {
                for (int index = 0; index < driverActualTransportView.actualTransportPackages.SelectedRows.Count; index++)
                {
                    DataGridViewRow row = driverActualTransportView.actualTransportPackages.SelectedRows[index];

                    Package package = Package.getById(int.Parse(row.Cells[0].Value.ToString()));

                    if (package.status == PackageStatus.NEW)
                    {
                        package.status = PackageStatus.RECEIVED;
                    }
                    else
                    {
                        package.status = PackageStatus.CLOSED;
                    }

                    package.save();
                }
            }

            showActualTransportPackages();
        }
    }
}
